package cn.itcast.manus.agent.db;

import cn.hutool.core.util.StrUtil;
import cn.itcast.manus.agent.BaseAgent;
import cn.itcast.manus.agent.prompt.PromptManagement;
import cn.itcast.manus.constants.Constant;
import cn.itcast.manus.dto.DialogMessageDTO;
import cn.itcast.manus.message.MessageSession;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import reactor.core.publisher.Flux;

import java.util.Map;

@Slf4j
public class DBAgent extends BaseAgent {

    @Resource
    private ChatClient chatClient;

    @Resource
    private PromptManagement promptManagement;

    private final MessageSession messageSession;

    public DBAgent(MessageSession messageSession) {
        this.messageSession = messageSession;
    }

    @Override
    protected String solve(String task) {
        var params = Map.of(Constant.TASK, task);
        var prompt = StrUtil.format(this.promptManagement.getPrompt(Constant.Prompts.MYSQL), params);

        // 调用聊天客户端生成流式响应内容
        var dbText = this.chatClient.prompt()
                .user(prompt)
                .call()
                .content();

        //3. 通过session发送信息给客户端
        var ms = DialogMessageDTO.builder()
                .text("[DBAgent]获取到MySQL数据库数据："+dbText)
                .build();
        this.messageSession.sendMessage(ms);

        //4. 返回内容给大模型
        return StrUtil.format("""
                [DBAgent]
                [DBAgent]获取到MySQL数据库数据：{}
                """, dbText);
    }

    @Override
    public ChatModel chatModel() {
        return null;
    }
}